@page "/"
@layout MainLayout
@inject HttpClient HttpClient
@inject NavigationManager NavigationManager
@inject PizzaSalesState SalesState
@inject PizzaSpecial pizzaSpecial
@using Microsoft.AspNetCore.WebUtilities

@using BlazingPizza.Services
@inject OrderState OrderState
@* 注入IJSRuntime *@
@inject IJSRuntime JavaScript

<div class="main">
    <ul class="pizza-cards">
        @if (specials != null && specials.Count > 0)
        {
            @foreach (var special in specials)
            {
                <li @onclick="@(()=>OrderState.ShowConfigurePizzaDialog(special))"
                    style="background-image: url('@special.ImageUrl')">
                    <div class="pizza-info">
                        <span class="title">@special.Name</span>
                        @special.Description
                        <span class="price">@special.GetFormattedBasePrice()</span>
                    </div>
                </li>
            }
        }
        else
        {
            <li>暂无数据...</li>
        }
    </ul>
</div>

@if (OrderState.ShowingConfigureDialog)
{
    <ConfigurePizzaDialog Pizza="OrderState.ConfiguringPizza" OnCancel="OrderState.CancelConfigurePizzaDialog"
        OnConfirm="OrderState.ConfirmConfigurePizzaDialog" />
}

<div class="sidebar">
    @if (order.Pizzas.Any())
    {
        <div class="order-contents">
            <h2>Your order</h2>

            @foreach (var configuredPizza in order.Pizzas)
            {
                <div class="cart-item">
                    <div class="title">@(configuredPizza.Size)" @configuredPizza.Special.Name</div>
                    <div class="item-price">
                        @configuredPizza.GetFormattedTotalPrice()
                    </div>
                    @* <a @onclick="@(() => OrderState.RemoveConfiguredPizza(configuredPizza))" class="delete-item">x</a> *@
                    <button type="button" class="close text-danger" aria-label="Close"
                        @onclick="@(async () => await RemovePizzaConfirmation(configuredPizza))">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
            }
        </div>
    }
    else
    {
        <div class="empty-cart">Choose a pizza<br>to get started</div>
    }

    <div class="order-total @(order.Pizzas.Any() ? "" : "hidden")">
        Total:
        <span class="total-price">@order.GetFormattedTotalPrice()</span>
        <a href="checkout"
            class="@(OrderState.Order.Pizzas.Count == 0 ? "btn btn-warning disabled" : "btn btn-warning")">
            Order >
        </a>
    </div>
</div>

@code {
    List<PizzaSpecial> specials = new();

    Order order => OrderState.Order;

    private string ToppingName { get; set; }

    //生命周期初始化
    protected override async Task OnInitializedAsync()
    {
        specials = await HttpClient.GetFromJsonAsync<List<PizzaSpecial>>(NavigationManager.BaseUri + "specials");
        //URL操作
        var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
        if (QueryHelpers.ParseQuery(uri.Query).TryGetValue("extratopping", out var extraTopping))
        {
            ToppingName = System.Convert.ToString(extraTopping);
            //对象转交给另一个组件
            NavigationManager.NavigateTo("buypizza");
        }
    }

    //删除确认，在C#代码中调用JS方法
    async Task RemovePizzaConfirmation(Pizza removePizza)
    {
        var messageParams = new
        {
            title = "Remove Pizza?",
            text = $"Do you want to remove the {removePizza.Special!.Name}from your order?",
            icon = "warning",
            buttons = new
            {
                abort = new { text = "No, leave it in my order", value = false },
                confirm = new { text = "Yes, remove pizza", value = true }
            },
            dangerMode = true
        };

        if (await JavaScript.InvokeAsync<bool>("swal", messageParams))
        {
            OrderState.RemoveConfiguredPizza(removePizza);
        }
    }

    //生命周期
    protected async override void OnInitialized()
    {
        await Task.Run(() => Console.WriteLine("OnInitialized..."));
    }

    protected async override Task OnParametersSetAsync()
    {
        await Task.Run(()=>Console.WriteLine("OnParametersSetAsync..."));
    }

    @* protected async override Task OnAfterRenderAsync()
    {
        await Task.Run(()=>Console.WriteLine("OnAfterRenderAsync..."));
    }

    //OnDisposeAsync override
    protected async override Task OnDisposeAsync()
    {
        await Task.Run(()=>Console.WriteLine("OnDisposeAsync..."));
    } *@
}